local achievementID = 1779
local cooking
local spellname = GetSpellInfo(51296)
local wasTracked
local criteria = {}
local frame = CreateFrame("Frame")
local enabled = true

local function UpdateCriteria()
	local _,_,_,completed = GetAchievementInfo(achievementID)
	if completed then
		DisableAddOn("GourmetHelper")
		print("Congratulations! You've completed The Northrend Gourmet. GourmetHelper has been disabled and will not be loaded any more in the future.")
		enabled = false
		return true
	end
	for i=1, GetAchievementNumCriteria(achievementID) do
		local food, _, completed = GetAchievementCriteriaInfo(achievementID, i)
		
		criteria[food] = not completed
	end
end

local function TagSkills()
	for i=1, TRADE_SKILLS_DISPLAYED do
		local label = _G["TradeSkillSkill"..i.."Text"]
		local text = label and label:GetText()
		if text then
			text = text:trim()
			if criteria[text] then
				label:SetText("* "..text)
			end
		end
	end
end

hooksecurefunc("TradeSkillFrame_Show", function()
	if enabled and TradeSkillFrameTitleText:GetText() == spellname then
		if UpdateCriteria() then
			return
		end
		
		wasTracked = IsTrackedAchievement(achievementID)
		if not wasTracked then
			AddTrackedAchievement(achievementID)
		end
		TagSkills()
		cooking = true
	end
end)

hooksecurefunc("TradeSkillFrame_Hide", function()
	if cooking then
		if not wasTracked then
			RemoveTrackedAchievement(achievementID)
		end
		cooking = false
	end
end)

frame:SetScript("OnEvent", function(self, event, ...)
	if enabled and TradeSkillFrameTitleText:GetText() == spellname then
		UpdateCriteria()
		TagSkills()
	end
end)
frame:RegisterEvent("TRACKED_ACHIEVEMENT_UPDATE")

hooksecurefunc("TradeSkillFrame_Update", function()
	if enabled and cooking then
		TagSkills()
	end
end)